﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NorthwindDataservice;
using System;
using System.Diagnostics;
using System.Net;
using System.Net.Http;
using System.Text;

namespace ConsoleClient
{
    class Program
    {
        private const string CategoriesApi = "http://localhost:5001/api/categories";

        static void Main(string[] args)
        {
            var dataService = new DataService();
            //foreach (var item in dataService.GetCategories())
            //{
            //    Console.WriteLine(item.Name);
            //}

            var service = new DataService();
            //var category = service.CreateCategory("Test", "CreateCategory_ValidData_CreteCategoryAndRetunsNewObject");

            //var orderDetails = service.GetOrderDetailsByOrderId(10248);

            //var orderDetails = service.GetOrderDetailsByProductId(11);

            //var order = service.GetOrder(10248);

            //var orderDetails = service.GetOrderDetailsByProductId(11);
            var cat = dataService.GetProductByCategory(1000001);
            Console.WriteLine(cat.Count);
            Console.ReadKey(true);

            //var product = service.GetProductByCategory(1);
            //Debug.Print($"{product}");
            //var product = service.GetProduct(1);

            //var orderDetails = service.GetOrderDetailsByOrderId(10248);

            //var newCategory = new
            //{
            //    Name = "Created",
            //    Description = ""
            //};
            //var (category, statusCode) = PostData(CategoriesApi, newCategory);
            //var statusCode = DeleteData($"{CategoriesApi}/-1");


            //Console.ReadLine();
        }

        static (JObject, HttpStatusCode) PostData(string url, object content)
        {
            var client = new HttpClient();
            string jsonContent = JsonConvert.SerializeObject(content);
            var requestContent = new StringContent(
                jsonContent,
                Encoding.UTF8,
                "application/json");
            var response = client.PostAsync(url, requestContent).Result;
            var data = response.Content.ReadAsStringAsync().Result;
            return ((JObject)JsonConvert.DeserializeObject(data), response.StatusCode);
        }

        static HttpStatusCode DeleteData(string url)
        {
            var client = new HttpClient();
            var response = client.DeleteAsync(url).Result;
            return response.StatusCode;
        }
    }
}
